package cn.kailangtu.commontools;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;

import java.util.Calendar;

public class TimeUtils {
    public static void setTime(Context context, Calendar calendar) {
        // 获取AlarmManager服务
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            // 设置时间
            long timeInMillis = calendar.getTimeInMillis();
            // 设置一次性闹钟
            Intent intent = new Intent("android.intent.action.SET_ALARM");
            PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
            alarmManager.setTime(timeInMillis);
            alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
        }
    }

    public static void setSystemTime(Context context, long timeInMillis) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            alarmManager.setTime(timeInMillis);
            Log.d("TimeUtils", "System time set to: " + timeInMillis);
        } else {
            Log.e("TimeUtils", "AlarmManager is null");
        }
    }




}
